/**
 * jims
 */
package com.jims.master.modules.doctor.service;

import java.text.SimpleDateFormat;
import java.util.*;

import com.jims.master.common.utils.PrintPdfUtils;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.doctor.dao.ClinicMasterDao;
import com.jims.master.modules.doctor.dao.PatMasterIndexDao;
import com.jims.master.modules.doctor.entity.ClinicMaster;
import com.jims.master.modules.doctor.entity.PatMasterIndex;
import com.jims.master.modules.sys.dao.CompanyDao;
import com.jims.master.modules.sys.entity.Company;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.service.CompanyService;
import com.jims.master.modules.sys.utils.DictUtils;
import com.jims.master.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.modules.doctor.entity.ClinicDeathProve;
import com.jims.master.modules.doctor.dao.ClinicDeathProveDao;

/**
 * Service
 * @author 系统管理员
 * @version 2017-07-16
 */
@Service
@Transactional(readOnly = true)
public class ClinicDeathProveService extends CrudService<ClinicDeathProveDao, ClinicDeathProve> {
	@Autowired
	PatMasterIndexDao patMasterIndexDao;
	@Autowired
	ClinicMasterDao clinicMasterDao;
	@Autowired
	CompanyDao companyDao;
	public ClinicDeathProve get(String id) {
		return super.get(id);
	}
	
	public List<ClinicDeathProve> findList(ClinicDeathProve clinicDeathProve) {
		return super.findList(clinicDeathProve);
	}
	
	public Page<ClinicDeathProve> finNdPage(Page<ClinicDeathProve> page, ClinicDeathProve clinicDeathProve) {
		return super.findPage(page, clinicDeathProve);
	}
	
	@Transactional(readOnly = false)
	public int save(ClinicDeathProve clinicDeathProve) {
		User user = UserUtils.getUser();
		if(clinicDeathProve.getPatMasterIndex()!=null&& StringUtils.isNotBlank(clinicDeathProve.getPatMasterIndex().getId())){
			clinicDeathProve.getPatMasterIndex().preUpdate();
			patMasterIndexDao.update(clinicDeathProve.getPatMasterIndex());
		}
		if(clinicDeathProve.getDeathNo()==null){
			int max=Integer.parseInt(dao.findMaxDeathNo()) + 1;
			String maxDeathNo1= String.format("%017d", max);
			clinicDeathProve.setDeathNo(maxDeathNo1);//死亡编号按规律计算
		}
		clinicDeathProve.setOrgId(user.getOrgId());
		clinicDeathProve.setInoroutFlag("0");
		return super.save(clinicDeathProve);
	}
	
	@Transactional(readOnly = false)
	public int delete(ClinicDeathProve clinicDeathProve) {
		return super.delete(clinicDeathProve);
	}
	@Transactional(readOnly = false)
	public ClinicDeathProve findByConditions(String clinicId,String patientId){
		return dao.findByConditions(clinicId, patientId);
	}
	/**
	 * 死亡证明打印
	 * @param clinicId
	 * @param patientId
	 * @return String
	 * @author 樊多
	 */
	public String clinicDeathProvePrint(String clinicId,String patientId){
		User user = UserUtils.getUser();
		Map map = new HashMap();
		List list = new ArrayList();
		String printPath = "";
		ClinicMaster clinicMaster=clinicMasterDao.get(clinicId);
		PatMasterIndex patMasterIndex= patMasterIndexDao.get(patientId);
		ClinicDeathProve clinicDeathProve=dao.findByConditions(clinicId, patientId);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdfhms = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Company company=companyDao.get(user.getOrgId());
		if(clinicDeathProve!=null){
			map.put("no",clinicDeathProve.getDeathNo());//编号
			String idType=DictUtils.getDictLabel(clinicDeathProve.getIdType(),"ID_TYPE_DICT","");
			map.put("idType",idType);//有效证件类别
			String degree=DictUtils.getDictLabel(clinicDeathProve.getDegree(),"DEGREE_DICT","");
			map.put("degree",degree);//文化程度
			map.put("deathDate",sdfhms.format(clinicDeathProve.getDeathDate()));//死亡日期
			String deathLocation=DictUtils.getDictLabel(clinicDeathProve.getDeathLocation(),"DEATH_LOCATION_DICT","");
			map.put("deathLocation",deathLocation);//死亡地点
			String gestation=DictUtils.getDictLabel(clinicDeathProve.getGestation(),"YES_NO","");
			map.put("gestation",gestation);//死亡时是否处于妊娠期或妊娠期终止42天内
			map.put("employer",clinicDeathProve.getEmployer());//生前工作单位
			map.put("aDisease",clinicDeathProve.getaDisease());//a死亡名称
			map.put("aDate",clinicDeathProve.getaDate());//a时间
			map.put("bDisease",clinicDeathProve.getbDisease());//b死亡名称
			map.put("bDate",clinicDeathProve.getbDate());//b时间
			map.put("cDisease",clinicDeathProve.getcDisease());//c死亡名称
			map.put("cDate",clinicDeathProve.getcDate());//c时间
			map.put("dDisease",clinicDeathProve.getdDisease());//d死亡名称
			map.put("dDate",clinicDeathProve.getdDate());//d时间
			map.put("otherDisease",clinicDeathProve.getOtherDisease());//other死亡名称
			map.put("otherDate",clinicDeathProve.getOtherDate());//other时间
			String topUnit=DictUtils.getDictLabel(clinicDeathProve.getTopUnit(),"TOP_UNIT_DICT","");
			map.put("topUnit",topUnit);//最高诊断单位
			String topBasis=DictUtils.getDictLabel(clinicDeathProve.getTopBasis(),"TOP_BASIS_DICT","");
			map.put("topBasis",topBasis);//最高诊断依据
		}
		map.put("orgCode",company.getOrgCode());//行政区域代码
		map.put("name",patMasterIndex.getName());//姓名
		String sex = DictUtils.getDictLabels(patMasterIndex.getSex(), "SEX_DICT", "");
		map.put("sex",sex);//性别
		String nation=DictUtils.getDictLabel(patMasterIndex.getNation(),"NATION_DICT","");
		map.put("nation",nation);//名族
		String nationality=DictUtils.getDictLabel(patMasterIndex.getNationality(),"NATIONALITY_DICT","");
		map.put("nationality",nationality);//国籍
		map.put("idNo",patMasterIndex.getIdNo());//证件号码
		map.put("age",patMasterIndex.getAge());//年龄
		String marriage=DictUtils.getDictLabel(patMasterIndex.getMarriage(),"MARRIAGE_DICT","");
		map.put("marriage", marriage);//婚姻状态
		if(patMasterIndex.getBirthdayDate()!=null){
			map.put("birthdayDate",sdf.format(patMasterIndex.getBirthdayDate()));//出生日期
		}
		String identity=DictUtils.getDictLabel(patMasterIndex.getIdentity(),"PROFESSION_DICT","");
		map.put("identity",identity);//个人身份
		map.put("nativePlace",patMasterIndex.getNativePlace());//户籍地址
		map.put("address",patMasterIndex.getAddress());//常住地址
		map.put("contacts",patMasterIndex.getContacts());//可联系的亲属姓名
		map.put("contactsPhone",patMasterIndex.getContactsPhone());//联系电话
		map.put("contactsAddress",patMasterIndex.getContactsAddress());//家庭住址或工作单位
		Calendar calendar =Calendar.getInstance();
		calendar.setTime(new Date());
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH)+1;
		int day=calendar.get(Calendar.DAY_OF_MONTH);
		map.put("year",year);//挂号时间
		map.put("month",month);//挂号时间
		map.put("day",day);//挂号时间
		list.add("hello");
		list.add("cost");
		printPath = PrintPdfUtils.previewPdf(user.getId(), "his/printDeathRecord.jasper", map, list, "printDeathRecord");
		return printPath;
	}
}